package com.formina.engine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.content.res.AssetManager;
import android.util.Log;


public class ObjReader {
	
	public static ModelInfo ReadObj(String file){
        AssetManager am = com.formina.engine.Engine.CONTEXT.getAssets();
        
        InputStream in;
		List<String> lines = new ArrayList<String>();

		try {
			in = am.open(file);
		
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String line = null;

			while ((line = reader.readLine()) != null)
				lines.add(line);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		float minx = 0,maxx = 0,miny=0,maxy=0,minz=0,maxz = 0;
		List<Float> Vertices = new ArrayList<Float>();
		List<Float> Normals = new ArrayList<Float>();
		List<Short>Indexes = new ArrayList<Short>();
		//fill arrays

		for(int x=0; x<lines.size();x++){
			String line = lines.get(x);
			if(line.length()>1){
				char first =line.charAt(0);
				char second=line.charAt(1);
				
				switch(first){
				case 'v':
					if(second=='n'){
						line = line.replace(first, ' ');
						line = line.replace(second, ' ');
 						line=line.trim();
 						String[] v =line.split("[ ]+");
 						for(int y=0; y<v.length;y++){
 							Normals.add(Float.valueOf(v[y]));
 						}
 					}else{
 						line = line.replace(first, ' ');
 						line=line.trim();
 						String[] v =line.split("[ ]+");
 						for(int y=0; y<v.length;y++){
 							//Log.d(String.valueOf(y), v[y]);
 							float f = Float.valueOf(v[y]);
 							Vertices.add(f);
 							switch(y){
 							case 0: //it's an x coordinate
 								if(f<minx){minx=f;}
 								if(f>maxx){maxx=f;}
 								break;
 							case 1: //it's a y coordinate
 								if(f<miny){miny=f;}
 								if(f>maxy){maxy=f;}
 								break;
 							case 2: //it's a z coordinate
 								if(f<minz){minz=f;}
 								if(f>maxz){maxz=f;}
 								break;
 							}
 						}
					}
					break;
				case 'f':
					line = line.replace(first, ' ');
					String[] v =line.split("[ ]+");
					String lin;
					for(int y=0; y<v.length;y++){
						lin =v[y];
						if(lin.length()>0){
							//Log.d(String.valueOf(y), v[y]);
							
							String[] vals=lin.split("[/]+");
							//Log.d("txt", vals[0]+"-"+vals[1]);
							Indexes.add((short)(Short.valueOf(vals[0])-1));
							//lastSIdx++; TextureCoords[lastSIdx]=Byte.valueOf(vals[1]);
						}
					}
					break;
				}
			}
		}

		//check vertices
		/*
		for(int x=0; x<Vertices.length;x++){
			Log.d("vertices["+String.valueOf(x)+"]", String.valueOf(Vertices[x]));
		}
		*/
		/*check normals
		for(int x=0; x<Normals.length;x++){
			Log.d("normals["+String.valueOf(x)+"]", String.valueOf(Normals[x]));
		}
		*/
		/*check texture coords
		for(int x=0; x<TextureCoords.length;x++){
			Log.d("TextureCoords["+String.valueOf(x)+"]", String.valueOf(TextureCoords[x]));
		}
		*/
		ModelInfo inf = new ModelInfo();
		
		float[] v = new float[Vertices.size()];
		float[] n = new float[Normals.size()];
		short[] i = new short[Indexes.size()];
		
		for(int z=0; z<Vertices.size();z++){
			v[z]=Vertices.get(z);
		}
		for(int z=0; z<Normals.size();z++){
			n[z]=Normals.get(z);
		}
		for(int z=0; z<Indexes.size();z++){
			i[z]=Indexes.get(z);
		}
		
		inf.Width = maxx-minx;
		inf.Height = maxy-miny;
		inf.Depth = maxz-minz;
		//Log.d("w/h/d", String.valueOf(inf.Width)+"/"+String.valueOf(inf.Height)+"/"+String.valueOf(inf.Depth));

		inf.Vertices = v;
		inf.Normals = n;
		inf.Indices = i;
		return inf;
	}
}
